package phi.engine.layers
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import phi.engine.PhiEngine;
	import phi.engine.actors.IActor;
	import phi.engine.core.GameTime;
	import phi.engine.scene.SceneManager;

	public class Layer extends Sprite
		implements ILayer
	{
		// Protected
		protected var engine :PhiEngine = PhiEngine.getInstance();
		
		// Private vars
		private var _actors :Array = [];
		
		/**
		 * Constructor.
		 */
		public function Layer()
		{
		}
		
		// -----------------------------------------------------------------------------
		// Public Get/Set funcitons
		// -----------------------------------------------------------------------------
		
		public function get actors():Array
		{
			return _actors.concat();
		}
		
		// -----------------------------------------------------------------------------
		// Public funcitons
		// -----------------------------------------------------------------------------
		
		public function addActor( actor:IActor ):void
		{
			if( !actor )
				return;
			
			if ( _actors.indexOf( actor ) == -1 )
			{
				_actors.push( actor );
				addChild( actor as DisplayObject );
				
				actor.layer = this;
			}
		}
		
		public function removeActor( actor:IActor ):void
		{
			if( !actor )
				return;
			
			if( _actors.indexOf( actor ) > -1 )
			{
				_actors.splice( _actors.indexOf( actor ), 1 );
				removeChild( actor as DisplayObject );
				
				actor.layer = null;
			}
		}
		
		public function getActorsByType( type:Class ):Array
		{
			var result :Array = [];
			
			for each( var actor :IActor in actors )
				if( actor is type )
					result.push( actor );
			
			return result;
		}
		
		public function update( gameTime:GameTime ):void
		{
			for each( var actor :IActor in actors )
				actor.update( gameTime );
		}
		
		public function draw( gameTime:GameTime ):void
		{
			//@Todo: layer draw here
			
			// Draw all actors
			for each( var actor :IActor in actors )
				actor.draw( gameTime );
		}
		
		public function handleInput():void
		{
			for each( var actor :IActor in actors )
				actor.handleInput();
		}
		
		
	}
}